package cn.globalph.housekeeper.utils

import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build

/**
 * 闹钟工具类
 */
object AlarmUtil{
    fun startAlarm(context: Context,alarmId:Int,time:Long,action:String){
        var intent = Intent()
        intent.action = action

        var alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager

        var sender = PendingIntent.getService(context,alarmId,intent,0)

        when {
            Build.VERSION.SDK_INT > Build.VERSION_CODES.M -> alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,time,sender)
            Build.VERSION.SDK_INT >Build.VERSION_CODES.LOLLIPOP -> {
                val alarmClockInfo = AlarmManager.AlarmClockInfo(time,sender)
                alarmManager.setAlarmClock(alarmClockInfo,sender)
            }
            Build.VERSION.SDK_INT >Build.VERSION_CODES.KITKAT -> alarmManager.setExact(AlarmManager.RTC_WAKEUP,time,sender)
            else -> alarmManager.set(AlarmManager.RTC_WAKEUP,time,sender)
        }
    }

    fun cancelAlarm(context: Context,alarmId: Int,action: String){
        var intent = Intent()
        intent.action = action
        var sender = PendingIntent.getService(context,alarmId,intent,0)
        var alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
        alarmManager.cancel(sender)
    }
}